package com.gxuwz.service;

import com.gxuwz.mapper.LeaveMapper;
import com.gxuwz.model.entity.Leave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class LeaveService {

    @Autowired
    private LeaveMapper leaveMapper;

    // 插入请假记录
    public int addLeave(Leave leave) {
        //生成请假编号
        String leaveID = generateLeaveId();
        //状态
        String status = "0";

        //插入的请假列表数据
        leave.setLeaveId(leaveID);
        leave.setApplytime(new Date());
        leave.setStatus(status);

        return leaveMapper.insertLeave(leave);
    }

    // 删除请假记录
    public int deleteLeaveByID(String leaveID) {
        return leaveMapper.deleteLeaveById(leaveID);
    }

    // 查询所有请假记录
    public List<Leave> getAllLeaves() {
        return leaveMapper.getAllLeaves();
    }
    //生成随机数
    public String generateLeaveId() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());
        Random random = new Random();
        int randomNum = 100 + random.nextInt(900); // 生成3位随机数
        return timestamp + randomNum;
    }
}